﻿using Dt261.Model;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dt261.db
{
    class PureToneResultDb
    {
        private readonly SQLiteConnection connection;

        public PureToneResultDb(string dbPath)
        {
            connection = new SQLiteConnection(dbPath);
            connection.CreateTable<PureToneResult>();
        }

        public int AddPureToneResult(PureToneResult m)
        {

            return connection.Insert(new PureToneResult()
            {
                
                Uid = m.Uid,
                MemberId = m.MemberId,


                Age = m.Age,
                AcLeftMaskedType =m.AcLeftMaskedType,
                AcLeftType=m.AcLeftType,
                AcRightMaskedType=m.AcRightMaskedType,
                AcRightType=m.AcRightType,

                BcLeftMaskedType=m.BcLeftMaskedType,
                BcLeftType=m.BcLeftType,
                BcRightMaskedType=m.BcRightMaskedType,
                BcRightType=m.BcRightType,

                Birthday = m.Birthday,
                CreateTime = DateTime.Now,
                DeafAidLeftModel = m.DeafAidLeftModel,
                DeafAidRightModel = m.DeafAidRightModel,
                DeafAidRoutine = m.DeafAidRoutine,
                DeafAidThreshold = m.DeafAidThreshold,
                DeafAidVolume = m.DeafAidVolume,
                DeviceWT = m.DeviceWT,


                Gender = m.Gender,
                Examiner=m.Examiner,
                HospitalId = m.HospitalId,

                IsUpload = m.IsUpload,

                ListenWord = m.ListenWord,
                Mark = m.Mark,
                Name = m.Name,

                SoundField = m.SoundField,
                Strength = m.Strength,
                TestType = m.TestType,
                Tinnitus = m.Tinnitus,

                State = m.State,
                ModifyDate = m.ModifyDate,

            });
        }

        public int UpdatePureToneResult(PureToneResult m)
        {

            IList<PureToneResult> list = connection.Query<PureToneResult>("select * from PureToneResult where id = ?", m.Id);

            if (list.Count() > 0)
            {
               return connection.Update(m);
            }

            else
            {
                return this.AddPureToneResult(m);
            }
        }

        public IList<PureToneResult> GetResults()
            => connection.Table<PureToneResult>().ToList();

        public IList<PureToneResult> GetPureToneResultById(int id)
        {
            IList<PureToneResult> list = connection.Query<PureToneResult>("select * from PureToneResult where id = ?", id);

            return list;
        }

        public IList<PureToneResult> GetPureToneResultByUid(string uid)
        {
            IList<PureToneResult> list = connection.Query<PureToneResult>("select * from PureToneResult where uid = ?", uid);

            return list;
        }

        public IList<PureToneResult> GetResultsByMemberId(string mid)
        {
            IList<PureToneResult> list = connection.Query<PureToneResult>("select * from PureToneResult where memberId = ? order by createTime DESC", mid);


            return list;
        }


        public void GetChildrenByUid(PureToneResult r)
        {
           // foreach (PureToneResult r in list)
            {
                r.DataSets = connection.Query<ResultDataSet>("select * from ResultDataSet where ResultId = ?", r.Uid);
                foreach (ResultDataSet s in r.DataSets)
                {
                    s.Entrys = connection.Query<ResultEntry>("select * from ResultEntry where DataSetId = ?", s.Uid);
                }
            }

        }

        public PureToneResult GetOneByUid(string uid)
        {
            IList<PureToneResult> list = GetPureToneResultByUid(uid);

            return list.Count() > 0 ? list[0] : null;
        }

        public PureToneResult GetOneById(int id)
        {
            IList<PureToneResult> list = GetPureToneResultById(id);

            return list.Count() > 0 ? list[0] : null;
        }

        public int DeletePureToneResult(PureToneResult m)
        {
            return connection.Delete(m);
        }

        public int DeletePureToneResultByUid(string uid)
        {
            PureToneResult item = GetOneByUid(uid);
            if (item != null)
                return connection.Delete(item);
            else
                return 0;
        }
    }
}
